package com.mhkj.supervise.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.supervise.dao.SuperviseProblemDao;
import com.mhkj.supervise.entity.SuperviseProblem;
import com.mhkj.supervise.service.SuperviseProblemService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.rbac.service.DictionaryService;

@Service
public class SuperviseProblemServiceImpl extends BaseServiceImpl<SuperviseProblem> implements SuperviseProblemService {
	@Resource
	private SuperviseProblemDao superviseProblemDao;
	@Resource
	private DictionaryService dictionaryService;

	@Resource
	public void setSuperviseProblemDao(SuperviseProblemDao SuperviseProblemDao) {
		super.setBaseRepository(SuperviseProblemDao);
	}

	@Override
	public Pagination<SuperviseProblem> listByPage(SuperviseProblem SuperviseProblem, int pageIndex, int size) {
		Specification<SuperviseProblem> spec = new Specification<SuperviseProblem>() {
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<SuperviseProblem> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];

				 Join<Object, Object> planNode = root.join("planNode",JoinType.INNER);
	 
			 
				if (!StringUtils.isEmpty(SuperviseProblem.getPlanNodeId())) {
					lstPredicates.add(criteriaBuilder.equal(planNode.get("id").as(Long.class),SuperviseProblem.getPlanNodeId()));
				}
				//获取
					lstPredicates.add(criteriaBuilder.equal(root.get("status").as(Integer.class),1));
 
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<SuperviseProblem> pageT = superviseProblemDao.findAll(spec,PageRequest.of(pageIndex, size, Sort.Direction.ASC, "id"));
		Pagination<SuperviseProblem> pagination = new Pagination<SuperviseProblem>();
		pagination.setTotal(superviseProblemDao.count(spec));
		pagination.setData(pageT.getContent());
		return pagination;
	}

	@Override
	public Pagination<SuperviseProblem> listRecordByPage(SuperviseProblem superviseProblem, int pageIndex,int pageSize) {
		Specification<SuperviseProblem> spec = new Specification<SuperviseProblem>() {
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<SuperviseProblem> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];

				 Join<Object, Object> planNode = root.join("planNode",JoinType.INNER);
				 Join<Object, Object> plan = planNode.join("plan",JoinType.INNER);
	 
			 
				if (!StringUtils.isEmpty(superviseProblem.getItemId())) {
					Join<Object, Object> item = plan.join("item",JoinType.INNER);
					lstPredicates.add(criteriaBuilder.equal(item.get("id").as(Long.class),superviseProblem.getItemId()));
				}
 
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<SuperviseProblem> pageT = superviseProblemDao.findAll(spec,PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "id"));
		Pagination<SuperviseProblem> pagination = new Pagination<SuperviseProblem>();
		pagination.setTotal(superviseProblemDao.count(spec));
		pagination.setData(pageT.getContent());
		return pagination;
	}

}
